import { ref, type Ref } from 'vue';
import { defineStore } from 'pinia';
// 仓库引入使用 util中axios使用示例
// LoginData 登录数据
const LoginData = defineStore('login', () => {
  const token: Ref<string> = ref('');
  const localStorageTokenKey: string = '_login_token_';
  const menuPermission = ref<any>();
  const username = ref('');
  /**
   * @method 设置token
   */
  function tokenSet(val: string) {
    localStorage.setItem(localStorageTokenKey, val);
    token.value = val;
  }
  /**
   * @method 获取token
   */
  function tokenGet(): string {
    if (token.value === '') {
      const localToken = localStorage.getItem(localStorageTokenKey);
      if (localToken !== null) token.value = localToken;
    }
    return token.value;
  }
  /**
   * @method 设置用户名
   */
  function setUserName(val: string) {
    username.value = val;
    localStorage.setItem('Fifth_DataEnter_Admin_Username', val);
  }
  /**
   * @method 获取用户名
   */
  function getUserName() {
    return localStorage.getItem('Fifth_DataEnter_Admin_Username');
  }
  /**
   * @method 设置菜单权限
   */
  function setMenuPermission(val: any) {
    menuPermission.value = val;
    localStorage.setItem('Fifth_DataEnter_Admin_MenuPermission', JSON.stringify(val));
  }
  /**
   * @method 获取菜单权限
   */
  function getMenuPermission() {
    return JSON.parse(localStorage.getItem('Fifth_DataEnter_Admin_MenuPermission') as any);
  }
  /**
   * @method 管理权限
   */
  const setPermission = (val: any) => {
    menuPermission.value = val;
    localStorage.setItem('Fifth_DataEnter_Admin_Permission', JSON.stringify(val));
  };
  /**
   * @method 管理权限
   */
  const getPermission = () => {
    return JSON.parse(localStorage.getItem('Fifth_DataEnter_Admin_Permission') as any);
  };

  return {
    tokenSet,
    tokenGet,
    setMenuPermission,
    setUserName,
    getUserName,
    getMenuPermission,
    setPermission,
    getPermission
  };
});

export default {
  LoginData
};
